﻿using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Customs.Components.KDT.SXXK.Info;
using Microsoft.ApplicationBlocks.Data;

namespace Customs.Components.KDT.SXXK
{
    public class NguyenPhuLieu : NguyenPhuLieuInfo
    {
        //---------------------------------------------------------------------------------------------
        public void Load(NguyenPhuLieuInfo entityInfo)
        {
            this._SoTiepNhan = entityInfo.SoTiepNhan;
            this._MaHaiQuan = entityInfo.MaHaiQuan;
            this._NamTiepNhan = entityInfo.NamTiepNhan;
            this._Ma = entityInfo.Ma;
            this._MaDoanhNghiep = entityInfo.MaDoanhNghiep;
            this._Ten = entityInfo.Ten;
            this._MaHS = entityInfo.MaHS;
            this._DVT_ID = entityInfo.DVT_ID;
            this._SoThuTuHang = entityInfo.SoThuTuHang;
        }

        //---------------------------------------------------------------------------------------------
        public int Delete(SqlTransaction transaction)
        {
            string query = string.Format("DELETE FROM DNPLDK_D WHERE SO_TNDKDT = {0} AND MA_HQTN = '{1}' AND MA_DV = '{2}' AND NAM_TN = {3} AND MA_NPL = '{4}'", this._SoTiepNhan, this.MaHaiQuan.PadRight(6), this._MaDoanhNghiep, this._NamTiepNhan, this._Ma);
            return SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query);
        }

        //-----------------------------------------------------------------------------------------
        public static void Delete(SqlTransaction transaction, long soTiepNhan, string maHaiQuan, string maDoanhNghiep, int namTiepNhan)
        {
            string query = string.Format("DELETE FROM DNPLDK_D WHERE SO_TNDKDT = {0} AND MA_HQTN = '{1}' AND MA_DV = '{2}' AND NAM_TN = {3}", soTiepNhan, maHaiQuan.PadRight(6), maDoanhNghiep, namTiepNhan);
            SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query);
        }

        //---------------------------------------------------------------------------------------------
        public int Delete()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString;
            string query = string.Format("DELETE FROM DNPLDK_D WHERE SO_TNDKDT = {0} AND MA_HQTN = '{1}' AND MA_DV = '{2}' AND NAM_TN = {3} AND MA_NPL = '{4}'", this._SoTiepNhan, this.MaHaiQuan.PadRight(6), this._MaDoanhNghiep, this._NamTiepNhan, this._Ma);
            return SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, query);
        }

        //---------------------------------------------------------------------------------------------
        public void Delete(SqlTransaction transaction, NguyenPhuLieuInfoCollection collection)
        {
            NguyenPhuLieu npl = new NguyenPhuLieu();
            foreach (NguyenPhuLieuInfo itemInfo in collection)
            {
                npl.Load(itemInfo);
                npl.Delete(transaction);
            }
        }

        //---------------------------------------------------------------------------------------------
        public bool Delete(NguyenPhuLieuInfoCollection collection)
        {
            bool returnValue;
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString))
            {
                conn.Open();
                SqlTransaction transaction = conn.BeginTransaction();

                try
                {
                    this.Delete(transaction, collection);
                    transaction.Commit();
                    returnValue = true;
                }
                catch
                {
                    returnValue = false;
                    transaction.Rollback();
                }
                finally
                {
                    conn.Close();
                }
            }
            return returnValue;
        }

        //---------------------------------------------------------------------------------------------
        public void InsertTransaction(SqlTransaction transaction, NguyenPhuLieuInfoCollection collection)
        {
            NguyenPhuLieu npl = new NguyenPhuLieu();
            foreach (NguyenPhuLieuInfo itemInfo in collection)
            {
                npl.Load(itemInfo);
                npl.InsertTransaction(transaction);
            }
        }

        //---------------------------------------------------------------------------------------------
        public void InsertTransaction(SqlTransaction transaction)
        {
            StringBuilder query = new StringBuilder();
            query.Append("INSERT INTO DNPLDK_D ( ");
            query.Append("So_TNDKDT, ");
            query.Append("Ma_HQTN, ");
            query.Append("Nam_TN, ");
            query.Append("MA_NPL, ");
            query.Append("Ma_DV, ");
            query.Append("TEN_NPL, ");
            query.Append("MA_HS, ");
            query.Append("MA_DVT, ");
            query.Append("STTHang ) ");

            query.Append("VALUES ( ");

            query.Append("@So_TNDKDT, ");
            query.Append("@Ma_HQTN, ");
            query.Append("@Nam_TN, ");
            query.Append("@MA_NPL, ");
            query.Append("@Ma_DV, ");
            query.Append("@TEN_NPL, ");
            query.Append("@MA_HS, ");
            query.Append("@MA_DVT, ");
            query.Append("@STTHang )");

            SqlParameter[] parameters = new SqlParameter[9];

            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = this._SoTiepNhan;

            parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
            parameters[1].Value = this._MaHaiQuan;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = this._NamTiepNhan;

            parameters[3] = new SqlParameter("@MA_NPL", SqlDbType.VarChar, 30);
            parameters[3].Value = this._Ma;

            parameters[4] = new SqlParameter("@Ma_DV", SqlDbType.VarChar, 14);
            parameters[4].Value = this._MaDoanhNghiep;

            parameters[5] = new SqlParameter("@TEN_NPL", SqlDbType.NVarChar, 80);
            parameters[5].Value = this._Ten;

            parameters[6] = new SqlParameter("@MA_HS", SqlDbType.VarChar, 12);
            parameters[6].Value = this._MaHS;

            parameters[7] = new SqlParameter("@MA_DVT", SqlDbType.Char, 3);
            parameters[7].Value = this._DVT_ID;

            parameters[8] = new SqlParameter("@STTHang", SqlDbType.Int);
            parameters[8].Value = this._SoThuTuHang;

            SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query.ToString(), parameters);
        }
    }
}